import { http } from "@/utils/http";

export type CommunityItem = {
  id: number;
  name: string;
  expirationTime: string;
  address: string;
  balance: number;
  createTime: string;
  updateTime: string;
  tenantId: number;
};

export type CommunityListResult = {
  code: number;
  data: CommunityItem[];
  message: string;
};

export type CommunityCreateParams = {
  name: string;
  address: string;
  expirationTime: string;
  balance?: number;
};

export type CommunityUpdateParams = {
  id: number;
  name: string;
  address: string;
  expirationTime: string;
  balance?: number;
};

/** 获取小区列表 */
export const getCommunityList = () => {
  return http.request<CommunityListResult>("get", "spCell/findAll");
};

/** 创建小区 */
export const createCommunity = (data: CommunityCreateParams) => {
  return http.request<{ code: number; message: string }>(
    "post",
    "spCell/create",
    { data }
  );
};

/** 更新小区 */
export const updateCommunity = (data: CommunityUpdateParams) => {
  return http.request<{ code: number; message: string }>(
    "put",
    "spCell/update",
    { data }
  );
};

/** 删除小区 */
export const deleteCommunity = (id: number) => {
  return http.request<{ code: number; message: string }>(
    "delete",
    `spCell/delete/${id}`
  );
};

/** 续费小区 */
export const renewCommunity = (id: number, expirationTime: string) => {
  return http.request<{ code: number; message: string }>(
    "put",
    "spCell/renew",
    {
      data: { id, expirationTime }
    }
  );
};
